package com.idroid.overlays;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;

import org.osmdroid.ResourceProxy;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.OverlayItem;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.idroid.R;
import com.idroid.domain.HookableParkingArea;
import com.idroid.domain.User;

public class HookableObjectsOverlay extends UpdateItemizedIconOverlay {
	private Drawable defaultMarker;
	private Resources resources;

	private float scaleFactor = 0.33f;

	public HookableObjectsOverlay(MapView mapView, Drawable defaultMarker,
			final int pFocusedBackgroundColor, Resources res,
			ResourceProxy proxy) {
		super(mapView, defaultMarker, pFocusedBackgroundColor, proxy);
		this.defaultMarker = defaultMarker;
		this.resources = res;
	}

	public HookableObjectsOverlay(MapView mapView, Drawable defaultMarker,
			Resources res, ResourceProxy proxy) {
		super(mapView, defaultMarker, proxy);
		this.defaultMarker = defaultMarker;
		this.resources = res;
	}

	public synchronized void addOverlayObjects(
			Collection<OverlayItem> hookableObjects) {

		super.removeAllItems();

		for (OverlayItem hookableObject : hookableObjects) {

			addHookableObject(hookableObject);
			// itemsToDelete.remove(hookableObject);
		}

		populate();

	}

	public synchronized void addOverlayObject(OverlayItem hookableObject) {

		super.removeAllItems();

		addHookableObject(hookableObject);

		populate();

	}

	public synchronized void addOverlayObjectNoRemove(OverlayItem hookableObject) {

		addHookableObject(hookableObject);

		populate();

	}

	static HashMap<String, BitmapDrawable> bitmapMap = new HashMap<String, BitmapDrawable>();

	private synchronized void addHookableObject(OverlayItem hookableObject) {

		Drawable image = defaultMarker;
		String symbol = "";
		if (hookableObject instanceof User) {
			symbol = ((User) hookableObject).getSymbol();
		} else if (hookableObject instanceof HookableParkingArea) {
			symbol = ((HookableParkingArea) hookableObject).getSymbol();
		}

		if (!bitmapMap.containsKey(symbol)) {

			try {
				String symId = symbol.replace("-", "_");
				Field symbolField = R.drawable.class.getField(symId);
				int symbolFieldId = symbolField.getInt(R.drawable.class);
				image = this.resources.getDrawable(symbolFieldId);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			Bitmap trackBitMap = ((BitmapDrawable) image).getBitmap();
			// create a matrix for the manipulation
			Matrix matrix = new Matrix();
			// resize the bit map

			matrix.postScale(scaleFactor, scaleFactor);

			Bitmap resizedBitmap = Bitmap.createBitmap(trackBitMap, 0, 0,
					trackBitMap.getWidth(), trackBitMap.getHeight(), matrix,
					true);
			// make a Drawable from Bitmap to allow to set the BitMap
			image = new BitmapDrawable(this.resources, resizedBitmap);
		} else {
			image = bitmapMap.get(symbol);
		}

		addDrawable(hookableObject, image);

	}

	public float getScaleFactor() {
		return scaleFactor;
	}

	public void setScaleFactor(float scaleFactor) {
		this.scaleFactor = scaleFactor;
	}

}
